<!--围栏地图页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">新增围栏</span>
    <el-button type="primary" style="float:right;margin-top:13px;"
               @click="router.push('/geofence')">返回</el-button>
  </div>
  <div id="mapContainer" style="width: 100%;height: 84vh;">

  </div>dict_option

  <el-dialog title="新建围栏" v-model="dialogVisible"
             :before-close="closeDialog">
    <el-form>
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入围栏名称" v-model="geofence.name"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="saveGeofence">确定</el-button>
    </template>
 </el-dialog>
</template>

<script setup>
import router from "@/router"
import {nextTick, onMounted, ref} from "vue";
import { DrawScene, DrawControl ,OperateEventType} from 'bmap-draw';
import {ElMessage} from "element-plus";
import axios from "axios";
import qs from "qs";

const geofence = ref({
  name: '',
  position: {}
});
const dialogVisible = ref(false);

var map;
onMounted(()=> {
  nextTick(() => {
    map = new BMapGL.Map("mapContainer");
    let point = new BMapGL.Point(116.404, 39.915)
    map.centerAndZoom(point, 15);


    //5.添加鼠标绘制控件
    //5.1 创建绘制场景类，此类事其他绘制控件的基类，负责获取覆盖物(围栏)，事件监听等，注意:需要导入！
    const scene = new DrawScene(map);
    //5.2 创建绘制控件类，它的构造方法需要传入绘制场景对象与配置对象,注意:需要导入！
    const drawContrl = new DrawControl(scene, {
      //5.3 绘制控件配置对象的配置信息 不显示图标用途提示+出现在右上角+显示圆形与矩形绘制按钮
      enableTips: false,
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      drawingItems: [
        'circle',
        'rectangle'
      ]
    });
    //6. 监听绘制完成时间，获取触发完成事件的电子围栏目标对象
    scene.addEventListener(OperateEventType.COMPLETE,(event)=>{
      dialogVisible.value = true;
      // console.log(event.target)
      let overlay = event.target.overlay;
      if (overlay instanceof BMapGL.Circle){
        geofence.value.position.type = 'circle';
        geofence.value.position.longitude = overlay.getCenter().lng;
        geofence.value.position.latitude = overlay.getCenter().lat;
        geofence.value.position.radius = overlay.getRadius();
      console.log(geofence.value)
    }else {
        geofence.value.position.type = 'rectangle';
        let recPoints = [];
        for (let i=0 ; i<4 ;i++){
          recPoints.push(
              overlay.points[i].latLng.lng+'-'+overlay.points[i].latLng.lat)
        }
        geofence.value.position.points = recPoints.join(',');
        console.log(geofence.value);
      }
    })
    //5.4 将准备好的绘制控件添加到地图对象中
    map.addControl(drawContrl);
  })
})
const closeDialog = ()=>{
  if(confirm('您确认要取消新增围栏吗?')){
    map.clearOverlays();
    geofence.value = {name:'',position:{}};
    dialogVisible.value = false;
  }
}
const saveGeofence = ()=>{
  if (!geofence.value.name || geofence.value.name.trim()==''){
    ElMessage.error("请输入围栏名称")
    return;
  }
  geofence.value.position = JSON.stringify(geofence.value.position);
  let data = qs.stringify(geofence.value);
  axios.post(BASE_URL+'/v1/geofence/save',data).then((response)=>{
    if (response.data.code == 2000){
      ElMessage.success('围栏新增成功');
      geofence.value = {name:'',position:{}};
      dialogVisible.value = false;
      router.push('/geofence')
    }else{
      ElMessage.error(response.data.message);
    }
  })
}

</script>

<style scoped>

</style>